<?php
namespace app\common\tool;

class Unidesk
{
    private $commomparams;

    public function __construct()
    {
        $this->commomparams = [
            'app_key'       => Config('unidesk.appkey'),
            'timestamp'     => date('Y-m-d H:i:s'),
            'v'             => '2.0',
            'sign_method'   => 'md5',
            'format'        => 'json',
            'simplify'      => true  
        ];
    }

    /**
     * 构造签名函数
     * @param array $params
     * @return string
     */
    protected function generate_sign($params)
    {
        ksort($params);
        $str = '';
        foreach($params as $key => $value){
            if(!is_array($value) && "@" != substr($value, 0, 1))
            {
                $str .= "$key$value";
            }
        }
        unset($key, $value);
        $sign_str = strtoupper( md5( Config('unidesk.appsecret') . $str . Config('unidesk.appsecret') ) );
        return $sign_str;
    }

    /**
     * 获取请求结果
     * @param array  $params
     */
    protected function get_request_result($params)
    {
        $url = Config('unidesk.domain').'?'.http_build_query($params);
        $ret = file_get_contents($url);
        return json_decode($ret,true);
    }

    /**
     * 换取access_token
     * @param string code
     */
    public function get_access_token($code)
    {
        try {
            $basicparams = [
                'method'    => 'taobao.top.auth.token.create',
                'code'      => $code
            ];
            $signparams = array_merge($this->commomparams,$basicparams);
            $signparams['sign'] = $this->generate_sign($signparams);
            $res = $this->get_request_result($signparams);
            if(isset($res['error_response']['sub_msg'])){
                throw new \Exception($res['error_response']['sub_msg']);    
            }
            if(isset($res['top_auth_token_create_response']['token_result'])){
                return json_decode($res['top_auth_token_create_response']['token_result'],true);
            }
            throw new \Exception('请求获取access_token失败!');
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * 广告主查询
     * @param string $sessionkey
     * @param array  $params
     */
    public function get_unidesk_advertiser($sessionkey,$pagesize,$page,$ids)
    {
        try {
            $basicparams = [
                'method'    => 'taobao.unidesk.rta.user.advertiser.get',
                'session'   => $sessionkey,
                'advertiser_top_query_dto' => json_encode([
                    'id'        => $ids, 
                    'page_no'   => $page,
                    'page_size' => $pagesize
                ])
            ];
            $signparams = array_merge($this->commomparams,$basicparams);
            $signparams['sign'] = $this->generate_sign($signparams);
            $res = $this->get_request_result($signparams);
            if(isset($res['error_response']['sub_msg'])){
                throw new \Exception($res['error_response']['sub_msg']);    
            }
            return $res;
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }  
    }

    /**
     * 获取全量广告主账户列表
     * @param string $sessionkey
     * @return array 
     */
    public function get_unidesk_advertiser_fulllist($sessionkey)
    {
        try {
            $data = [];
            $page = 1;
            do {
                $ads = $this->get_unidesk_advertiser($sessionkey,10000,$page,[]);
                if(!empty($ads['unidesk_rta_user_advertiser_get_response']['result']['advertiser_top_d_t_o'])){
                    $res = $ads['unidesk_rta_user_advertiser_get_response']['result']['advertiser_top_d_t_o'];
                    foreach ($res as $key => $value) {
                        $data[] = $value;     
                    }
                }else{
                    break;
                }
                $page++;    
            } while (true);
            return $data;
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * 计划组分页数据
     * @param int    $pagesize          每页条数
     * @param int    $page              页码
     * @param string $advertiser_id     广告组id
     * @param string $sessionkey        access_token
     * @param string $campaign_id       计划组id
     * @return array 
     */
    public function get_unidesk_campaign($sessionkey,$advertiser_id,$pagesize,$page,$campaign_id)
    {
        try {
            $basicparams = [
                'method'    => 'taobao.unidesk.rta.ad.campaign.get',
                'session'   => $sessionkey,    
            ];
            if(!empty($campaign_id) && is_numeric($campaign_id)){
                $basicparams['campaign_top_query_dto'] = json_encode([
                    'advertiser_id' => $advertiser_id,
                    'ids'           => [$campaign_id],
                    'page_no'       => $page,
                    'page_size'     => $pagesize,
                ]);    
            }else{
                $basicparams['campaign_top_query_dto'] = json_encode([
                    'advertiser_id' => $advertiser_id,
                    'page_no'       => $page,
                    'page_size'     => $pagesize,
                ]);  
            }
            $signparams = array_merge($this->commomparams,$basicparams);
            $signparams['sign'] = $this->generate_sign($signparams);
            $res = $this->get_request_result($signparams);
            if(isset($res['error_response']['msg'])){
                throw new \Exception($res['error_response']['msg']);    
            }
            if(!empty($res['unidesk_rta_ad_campaign_get_response']['result'])){
                return [
                    'count'    => $res['unidesk_rta_ad_campaign_get_response']['total'],
                    'pagecount'=> ceil($res['unidesk_rta_ad_campaign_get_response']['total']/$pagesize),
                    'pagesize' => $pagesize,
                    'page'     => $page,
                    'data'     => $res['unidesk_rta_ad_campaign_get_response']['result']['campaign_top_d_t_o']    
                ];
            }else{
                return [
                    'count'     => 0,     
                    'pagecount' => 0,     
                    'pagesize'  => $pagesize,     
                    'page'      => $page,     
                    'data'      => [],     
                ];
            }
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * 项目分页数据
     * @param int    $pagesize          每页条数
     * @param int    $page              页码
     * @param string $advertiser_id     广告组id
     * @param string $sessionkey        access_token
     * @param string $campaign_id       计划组id
     * @param string $media             媒体id:toutiao
     * @return array 
     */
    public function get_unidesk_project($sessionkey,$advertiser_id,$pagesize,$page,$campaign_id)
    {
        try {
            $basicparams = [
                'method'    => 'taobao.unidesk.rta.ad.campaign.get',
                'session'   => $sessionkey,    
            ];
            if(!empty($campaign_id) && is_numeric($campaign_id)){
                $basicparams['campaign_top_query_dto'] = json_encode([
                    'advertiser_id' => $advertiser_id,
                    'ids'           => [$campaign_id],
                    'page_no'       => $page,
                    'page_size'     => $pagesize,
                    'version'       => 9999,
                ]);    
            }else{
                $basicparams['campaign_top_query_dto'] = json_encode([
                    'advertiser_id' => $advertiser_id,
                    'page_no'       => $page,
                    'page_size'     => $pagesize,
                    'version'       => 9999,
                ]);  
            }
            $signparams = array_merge($this->commomparams,$basicparams);
            $signparams['sign'] = $this->generate_sign($signparams);
            $res = $this->get_request_result($signparams);
            if(isset($res['error_response']['msg'])){
                throw new \Exception($res['error_response']['msg']);    
            }
            if(!empty($res['unidesk_rta_ad_campaign_get_response']['result'])){
                return [
                    'count'    => $res['unidesk_rta_ad_campaign_get_response']['total'],
                    'pagecount'=> ceil($res['unidesk_rta_ad_campaign_get_response']['total']/$pagesize),
                    'pagesize' => $pagesize,
                    'page'     => $page,
                    'data'     => $res['unidesk_rta_ad_campaign_get_response']['result']['campaign_top_d_t_o']    
                ];
            }else{
                return [
                    'count'     => 0,     
                    'pagecount' => 0,     
                    'pagesize'  => $pagesize,     
                    'page'      => $page,     
                    'data'      => [],     
                ];
            }
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * 计划分页数据
     * @param int    $pagesize          每页条数
     * @param int    $page              页码
     * @param string $advertiser_id     广告主id
     * @param string $sessionkey        access_token
     * @param string $campaign_id       计划组id
     * @param string $adgroup_id        计划id
     * @return array 
     */
    public function get_unidesk_adgroup($sessionkey,$advertiser_id,$pagesize,$page,$campaign_id,$adgroup_id)
    {
        try {
            $basicparams = [
                'method'    => 'taobao.unidesk.rta.ad.adgroup.get',
                'session'   => $sessionkey,    
            ];
            $adgroup_top_query_dto = [
                'advertiser_id' => $advertiser_id,
                'page_no'       => $page,
                'page_size'     => $pagesize,
            ];
            if(!empty($campaign_id) && is_numeric($campaign_id)){
                $adgroup_top_query_dto['campaign_id'] = $campaign_id;  
            }
            if(!empty($adgroup_id) && is_numeric($adgroup_id)){
                $adgroup_top_query_dto['ids'] = [$adgroup_id];
            }
            $basicparams['adgroup_top_query_dto'] = json_encode($adgroup_top_query_dto);
            $signparams = array_merge($this->commomparams,$basicparams);
            $signparams['sign'] = $this->generate_sign($signparams);
            $res = $this->get_request_result($signparams);
            if(isset($res['error_response']['msg'])){
                throw new \Exception($res['error_response']['msg']);    
            }
            if(!empty($res['unidesk_rta_ad_adgroup_get_response']['result'])){
                return [
                    'count'    => $res['unidesk_rta_ad_adgroup_get_response']['total'],
                    'pagecount'=> ceil($res['unidesk_rta_ad_adgroup_get_response']['total']/$pagesize),
                    'pagesize' => $pagesize,
                    'page'     => $page,
                    'data'     => $res['unidesk_rta_ad_adgroup_get_response']['result']['adgroup_top_d_t_o']    
                ];
            }else{
                return [
                    'count'     => 0,     
                    'pagecount' => 0,     
                    'pagesize'  => $pagesize,     
                    'page'      => $page,     
                    'data'      => [],     
                ];
            }
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * 广告分页数据
     * @param int    $pagesize          每页条数
     * @param int    $page              页码
     * @param string $advertiser_id     广告主id
     * @param string $sessionkey        access_token
     * @param string $campaign_id       计划组id
     * @param string $adgroup_id        计划id
     * @return array 
     */
    public function get_unidesk_promotion($sessionkey,$advertiser_id,$pagesize,$page,$campaign_id,$adgroup_id)
    {
        try {
            $basicparams = [
                'method'    => 'taobao.unidesk.rta.ad.adgroup.get',
                'session'   => $sessionkey,    
            ];
            $adgroup_top_query_dto = [
                'advertiser_id' => $advertiser_id,
                'page_no'       => $page,
                'page_size'     => $pagesize,
                'version'       => 9999,
            ];
            if(!empty($campaign_id) && is_numeric($campaign_id)){
                $adgroup_top_query_dto['campaign_id'] = $campaign_id;  
            }
            if(!empty($adgroup_id) && is_numeric($adgroup_id)){
                $adgroup_top_query_dto['ids'] = [$adgroup_id];
            }
            $basicparams['adgroup_top_query_dto'] = json_encode($adgroup_top_query_dto);
            $signparams = array_merge($this->commomparams,$basicparams);
            $signparams['sign'] = $this->generate_sign($signparams);
            $res = $this->get_request_result($signparams);
            if(isset($res['error_response']['msg'])){
                throw new \Exception($res['error_response']['msg']);    
            }
            if(!empty($res['unidesk_rta_ad_adgroup_get_response']['result'])){
                return [
                    'count'    => $res['unidesk_rta_ad_adgroup_get_response']['total'],
                    'pagecount'=> ceil($res['unidesk_rta_ad_adgroup_get_response']['total']/$pagesize),
                    'pagesize' => $pagesize,
                    'page'     => $page,
                    'data'     => $res['unidesk_rta_ad_adgroup_get_response']['result']['adgroup_top_d_t_o']    
                ];
            }else{
                return [
                    'count'     => 0,     
                    'pagecount' => 0,     
                    'pagesize'  => $pagesize,     
                    'page'      => $page,     
                    'data'      => [],     
                ];
            }
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * 创意分页数据
     * @param int    $pagesize          每页条数
     * @param int    $page              页码
     * @param string $advertiser_id     广告主id
     * @param string $sessionkey        access_token
     * @param string $creative_id       创意id
     * @return array 
     */
    public function get_unidesk_creative($sessionkey,$advertiser_id,$pagesize,$page,$creative_id)
    {
        try {
            $basicparams = [
                'method'    => 'taobao.unidesk.rta.ad.creative.get',
                'session'   => $sessionkey,    
            ];
            if(!empty($creative_id) && is_numeric($creative_id)){
                $basicparams['creative_top_query_dto'] = json_encode([
                    'advertiser_id' => $advertiser_id,
                    'ids'           => [$creative_id],
                    'page_no'       => $page,
                    'page_size'     => $pagesize
                ]);    
            }else{
                $basicparams['creative_top_query_dto'] = json_encode([
                    'advertiser_id' => $advertiser_id,
                    'page_no'       => $page,
                    'page_size'     => $pagesize
                ]);  
            }
            $signparams = array_merge($this->commomparams,$basicparams);
            $signparams['sign'] = $this->generate_sign($signparams);
            $res = $this->get_request_result($signparams);
            if(isset($res['error_response']['msg'])){
                throw new \Exception($res['error_response']['msg']);    
            }
            if(!empty($res['unidesk_rta_ad_creative_get_response']['result'])){
                return [
                    'count'    => $res['unidesk_rta_ad_creative_get_response']['total'],
                    'pagecount'=> ceil($res['unidesk_rta_ad_creative_get_response']['total']/$pagesize),
                    'pagesize' => $pagesize,
                    'page'     => $page,
                    'data'     => $res['unidesk_rta_ad_creative_get_response']['result']['creative_top_d_t_o']    
                ]; 
            }else{
                return [
                    'count'     => 0,     
                    'pagecount' => 0,     
                    'pagesize'  => $pagesize,     
                    'page'      => $page,     
                    'data'      => [],     
                ];    
            }
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * 素材分页数据
     * @param int    $pagesize          每页条数
     * @param int    $page              页码
     * @param string $advertiser_id     广告主id
     * @param string $sessionkey        access_token
     * @param string $material_id       素材id
     * @param int    $type              素材类型 1：图片，2：视频
     */
    public function get_unidesk_material($sessionkey,$advertiser_id,$pagesize,$page,$material_id,$type)
    {
        try {
            $basicparams = [
                'method'    => 'taobao.unidesk.rta.assets.material.get',
                'session'   => $sessionkey,    
            ];
            $arr = [
                'advertiser_id' => $advertiser_id,
                'page_no'       => $page,
                'page_size'     => $pagesize,
            ];
            if(!empty($material_id) && is_numeric($material_id)){
                $arr['ids']     = [$material_id];  
            }
            if(!empty($type) && is_numeric($type)){
                $arr['type']    = $type;
            }
            $basicparams['material_top_query_dto'] = json_encode($arr);
            $signparams = array_merge($this->commomparams,$basicparams);
            $signparams['sign'] = $this->generate_sign($signparams);
            $res = $this->get_request_result($signparams);
            if(isset($res['error_response']['msg'])){
                throw new \Exception($res['error_response']['msg']);    
            }
            if(!empty($res['unidesk_rta_assets_material_get_response']['result'])){
                return [
                    'count'    => $res['unidesk_rta_assets_material_get_response']['total'],
                    'pagecount'=> ceil($res['unidesk_rta_assets_material_get_response']['total']/$pagesize),
                    'pagesize' => $pagesize,
                    'page'     => $page,
                    'data'     => $res['unidesk_rta_assets_material_get_response']['result']['material_top_d_t_o']    
                ];
            }else{
                return [
                    'count'     => 0,     
                    'pagecount' => 0,     
                    'pagesize'  => $pagesize,     
                    'page'      => $page,     
                    'data'      => [],     
                ];    
            }
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * @param string $sessionkey        access_token
     * @param string $advertiser_id     广告主id
     */
    public function get_full_material_list($sessionkey,$advertiser_id)
    {
        try {
            $page = 1;
            $data = [];
            do {
                $ads = $this->get_unidesk_material($sessionkey,$advertiser_id,100,$page,'','');
                if(empty($ads['data'])){
                    break;
                }
                foreach ($ads['data'] as $key => $value) {
                    $data[$value['id']]['id']   = $value['id'];               
                    $data[$value['id']]['name'] = isset($value['name']) ? $value['name'] : '';               
                }
                $page++;    
            } while (true);
            return array_values($data);
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * 获取产品主图素材,仅图片
     * @param string $sessionkey        access_token
     * @param string $advertiser_id     广告主id
     */
    public function get_full_goods_material_list($sessionkey,$advertiser_id,$type)
    {
        try {
            $page = 1;
            $data = [];
            do {
                $ads = $this->get_unidesk_material($sessionkey,$advertiser_id,100,$page,'',$type);
                if(empty($ads['data'])){
                    break;
                }
                foreach ($ads['data'] as $key => $value) {
                    $data[$value['id']] = $value;              
                }
                $page++;    
            } while (true);
            return array_values($data);
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * @param string $sessionkey        access_token
     * @param string $advertiser_id     广告主id
     */
    public function get_full_campaign_list($sessionkey,$advertiser_id)
    {
        try {
            $page = 1;
            $data = [];
            do {
                $ads = $this->get_unidesk_campaign($sessionkey,$advertiser_id,100,$page,'','');
                if(empty($ads['data'])){
                    break;
                }
                foreach ($ads['data'] as $key => $value) {
                    $data[$value['id']]['id']   = $value['id'];               
                    $data[$value['id']]['name'] = isset($value['name']) ? $value['name'] : '';               
                }
                $page++;    
            } while (true);
            return array_values($data);     
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * @param string $sessionkey        access_token
     * @param string $advertiser_id     广告主id
     */
    public function get_full_plan_list($sessionkey,$advertiser_id)
    {
        try {
            $page = 1;
            $data = [];
            do {
                $ads = $this->get_unidesk_adgroup($sessionkey,$advertiser_id,100,$page,'','');
                if(empty($ads['data'])){
                    break;
                }
                foreach ($ads['data'] as $key => $value) {
                    $data[$value['id']]['id']   = $value['id'];               
                    $data[$value['id']]['name'] = isset($value['name']) ? $value['name'] : '';               
                }
                $page++;    
            } while (true);
            return array_values($data);     
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * @param string $sessionkey        access_token
     * @param string $advertiser_id     广告主id
     */
    public function get_full_project_list($sessionkey,$advertiser_id)
    {
        try {
            $page = 1;
            $data = [];
            do {
                $ads = $this->get_unidesk_project($sessionkey,$advertiser_id,100,$page,'');
                if(empty($ads['data'])){
                    break;
                }
                foreach ($ads['data'] as $key => $value) {
                    $data[$value['id']]['id']   = $value['id'];               
                    $data[$value['id']]['name'] = isset($value['name']) ? $value['name'] : '';               
                }
                $page++;    
            } while (true);
            return array_values($data);     
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * @param string $sessionkey        access_token
     * @param string $advertiser_id     广告主id
     */
    public function get_full_promotion_list($sessionkey,$advertiser_id)
    {
        try {
            $page = 1;
            $data = [];
            do {
                $ads = $this->get_unidesk_promotion($sessionkey,$advertiser_id,20,$page,'','');
                if(empty($ads['data'])){
                    break;
                }
                foreach ($ads['data'] as $key => $value) {
                    $data[$value['id']]['id']   = $value['id'];               
                    $data[$value['id']]['name'] = isset($value['name']) ? $value['name'] : '';               
                }
                $page++;    
            } while (true);
            return array_values($data);     
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * @param string $sessionkey        access_token
     * @param string $advertiser_id     广告主id
     */
    public function get_full_creative_list($sessionkey,$advertiser_id)
    {
        try {
            $page = 1;
            $data = [];
            do {
                $ads = $this->get_unidesk_creative($sessionkey,$advertiser_id,100,$page,'','');
                if(empty($ads['data'])){
                    break;
                }
                foreach ($ads['data'] as $key => $value) {
                    $data[$value['id']]['id']   = $value['id'];               
                    $data[$value['id']]['name'] = isset($value['title']) ? $value['title'] : '';                
                }
                $page++;    
            } while (true);
            return array_values($data);    
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * 获取日报数据
     * @param string    $sessionkey
     * @param int       $adid
     * @param string    $start
     * @param string    $end
     */
    public function get_unidesk_daily_report($sessionkey, $adid, $start, $end, $groupby, $effect, $effect_type)
    {
        try {
            $basicparams = [
                'method'    => 'taobao.unidesk.rta.report.daily.get',
                'session'   => $sessionkey,    
            ];
            $arr = [
                'advertiser_id' => $adid,
                'group_by'      => $groupby,
                'end_date'      => $start,
                'start_date'    => $end,
                // 'version'       => 9999,
                'fields'        => [
                                    "favoriteStores",
                                    "playNums99",
                                    "wifiPlayPercent",
                                    "preAlipayVolume ",
                                    "collectionCost",
                                    "searchCost",
                                    "convert",
                                    "effectPlayRate",
                                    "directPreAlipayAmount",
                                    "adgroupId",
                                    "directTransactionAmount",
                                    "liveRechargeNums",
                                    "shareNums",
                                    "directReturnOnInvestment",
                                    "day ",
                                    "alipayCost",
                                    "pageArriveUv",
                                    "pageArrive",
                                    "adPv",
                                    "directPreAlipayVolume",
                                    "campaignId",
                                    "directAlipayCost",
                                    "returnOnInvestment",
                                    "liveWatchNums",
                                    "click",
                                    "directTakeOrderAmount ",
                                    "visitTime",
                                    "playOverNums",
                                    "ecpm",
                                    "finishPlayRate",
                                    "visitCost ",
                                    "effectPlayCost",
                                    "playNums50 ",
                                    "takeOrderVolume",
                                    "ecpc",
                                    "visitVolume",
                                    "favoriteBabyVolume",
                                    "effectPlayNums",
                                    "liveInteractionNums",
                                    "directTransactionVolume",
                                    "creativeId ",
                                    "directAddCartVolume",
                                    "convertRate",
                                    "transactionAmount",
                                    "preAlipayAmount ",
                                    "liveLikeNums",
                                    "liveRechargeAmount",
                                    "likeNums",
                                    "liveAverageWatchTime ",
                                    "visitPageVolume",
                                    "buyCost",
                                    "liveFollowNums",
                                    "searchVolume",
                                    "cost",
                                    "directFavoriteBabyVolume",
                                    "playNums25",
                                    "playNums50",
                                    "transactionVolume",
                                    "averagePlayTime",
                                    "liveCommentNums",
                                    "advertiserId",
                                    "directTakeOrderVolume",
                                    "takeOrderAmount",
                                    "addCartVolume",
                                    "playNums",
                                    "depthStoreVolume",
                                    "adCtr",
                                    "convertCost ",
                                    "playNums75",
                                    "play3sCount",
                                    "play5sCount",
                                    "play7sCount",
                                    "platformPageViewCount",
                                    "lanButtonClickCount",
                                    "lanJumpButtonClickers",
                                    "lanJumpButtonClickRate"
                                ],
                'filtering'     => [
                    'effect'        =>  $effect,
                    'effect_type'   =>  $effect_type,
                ]
            ];
            $basicparams['report_top_query_dto'] = json_encode($arr);
            $signparams = array_merge($this->commomparams,$basicparams);
            $signparams['sign'] = $this->generate_sign($signparams);
            $res = $this->get_request_result($signparams);
            if(isset($res['error_response']['msg'])){
                throw new \Exception($res['error_response']['msg']);    
            }
            if(!empty($res['unidesk_rta_report_daily_get_response']['result']['report_top_d_t_o'][0])){
                return $res['unidesk_rta_report_daily_get_response']['result']['report_top_d_t_o'][0];
            }
            return [];    
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * 创意组件查询
     * 
     */
    public function get_unidesk_creativecomponent($sessionkey,$advertiser_id,$pagesize,$page,$creativecomponent_id,$type)
    {
        try {
            $basicparams = [
                'method'    => 'taobao.unidesk.rta.assets.creativecomponent.get',
                'session'   => $sessionkey,    
            ];
            $creative_component_top_query_dto = [
                'advertiser_id' => $advertiser_id,
                'page_no'       => $page,
                'page_size'     => $pagesize,
            ];
            if(!empty($creativecomponent_id) && is_numeric($creativecomponent_id)){
                $creative_component_top_query_dto['id'] = $creativecomponent_id;  
            }
            if(!empty($type) && is_numeric($type)){
                $creative_component_top_query_dto['type'] = $type;
            }
            $basicparams['creative_component_top_query_dto'] = json_encode($creative_component_top_query_dto);
            $signparams = array_merge($this->commomparams,$basicparams);
            $signparams['sign'] = $this->generate_sign($signparams);
            $res = $this->get_request_result($signparams);
            if(isset($res['error_response']['msg'])){
                throw new \Exception($res['error_response']['msg']);    
            }
            if(!empty($res['unidesk_rta_assets_creativecomponent_get_response']['result'])){
                return [
                    'count'    => $res['unidesk_rta_assets_creativecomponent_get_response']['total'],
                    'pagecount'=> ceil($res['unidesk_rta_assets_creativecomponent_get_response']['total']/$pagesize),
                    'pagesize' => $pagesize,
                    'page'     => $page,
                    'data'     => $res['unidesk_rta_assets_creativecomponent_get_response']['result']['creative_component_top_d_t_o']    
                ];
            }else{
                return [
                    'count'     => 0,     
                    'pagecount' => 0,     
                    'pagesize'  => $pagesize,     
                    'page'      => $page,     
                    'data'      => [],     
                ];
            }        
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * 条件查询计划列表
     * @param string $access_token
     * @param string $advertiser_id
     * @param string $exact
     */
    public function query_plan_by_id_name($access_token,$advertiser_id,$exact,$pagesize,$page)
    {
        try {
            $basicparams = [
                'method'    => 'taobao.unidesk.rta.ad.adgroup.get',
                'session'   => $access_token,    
            ];
            $adgroup_top_query_dto = [
                'advertiser_id' => $advertiser_id,
                'page_size'     => $pagesize,
                'page_no'       => $page,
            ];
            if(!empty($exact) && is_numeric($exact)){
                $adgroup_top_query_dto['ids'] = [$exact];
            }
            if(!empty($exact) && !is_numeric($exact)){
                $adgroup_top_query_dto['name'] = $exact;
            }
            $basicparams['adgroup_top_query_dto'] = json_encode($adgroup_top_query_dto);
            $signparams = array_merge($this->commomparams,$basicparams);
            $signparams['sign'] = $this->generate_sign($signparams);
            $res = $this->get_request_result($signparams);
            if(isset($res['error_response']['msg'])){
                throw new \Exception($res['error_response']['msg']);    
            }
            if(!empty($res['unidesk_rta_ad_adgroup_get_response']['result']['adgroup_top_d_t_o'] )){
                return [
                    'count'    => $res['unidesk_rta_ad_adgroup_get_response']['total'],
                    'pagecount'=> ceil($res['unidesk_rta_ad_adgroup_get_response']['total']/$pagesize),
                    'pagesize' => $pagesize,
                    'page'     => $page,
                    'data'     => $res['unidesk_rta_ad_adgroup_get_response']['result']['adgroup_top_d_t_o']    
                ];
            }else{
                return [
                    'count'     => 0,     
                    'pagecount' => 0,     
                    'pagesize'  => $pagesize,     
                    'page'      => $page,     
                    'data'      => [],     
                ];
            }
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * 条件查询广告列表
     * @param string $access_token
     * @param string $advertiser_id
     * @param string $exact
     */
    public function query_promotion_by_id_name($access_token,$advertiser_id,$exact,$pagesize,$page)
    {
        try {
            $basicparams = [
                'method'    => 'taobao.unidesk.rta.ad.adgroup.get',
                'session'   => $access_token,    
            ];
            $adgroup_top_query_dto = [
                'advertiser_id' => $advertiser_id,
                'page_size'     => $pagesize,
                'page_no'       => $page,
                'version'       => 9999,
            ];
            if(!empty($exact) && is_numeric($exact)){
                $adgroup_top_query_dto['ids'] = [$exact];
            }
            if(!empty($exact) && !is_numeric($exact)){
                $adgroup_top_query_dto['name'] = $exact;
            }
            $basicparams['adgroup_top_query_dto'] = json_encode($adgroup_top_query_dto);
            $signparams = array_merge($this->commomparams,$basicparams);
            $signparams['sign'] = $this->generate_sign($signparams);
            $res = $this->get_request_result($signparams);
            if(isset($res['error_response']['msg'])){
                throw new \Exception($res['error_response']['msg']);    
            }
            if(!empty($res['unidesk_rta_ad_adgroup_get_response']['result']['adgroup_top_d_t_o'] )){
                return [
                    'count'    => $res['unidesk_rta_ad_adgroup_get_response']['total'],
                    'pagecount'=> ceil($res['unidesk_rta_ad_adgroup_get_response']['total']/$pagesize),
                    'pagesize' => $pagesize,
                    'page'     => $page,
                    'data'     => $res['unidesk_rta_ad_adgroup_get_response']['result']['adgroup_top_d_t_o']    
                ];
            }else{
                return [
                    'count'     => 0,     
                    'pagecount' => 0,     
                    'pagesize'  => $pagesize,     
                    'page'      => $page,     
                    'data'      => [],     
                ];
            }
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }
}